﻿using Hangfire;
using LiveShopvnext.Commods;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Threading;

namespace LiveShopvnext.Hangfire
{
    public class HangfireService : BackgroundService
    {
        private readonly IRepository<FreeActions, Guid> _repository;
        private readonly IRepository<CommodInfo, Guid> _repository0;
        private readonly IRepository<CommodityType, Guid> _repository1;
        private readonly IRepository<CommodSpecifica, Guid> _repository2;
        private readonly IRepository<CommodOrder, Guid> _repository3;
        private readonly IRepository<UserOrder, Guid> _repository4;
        private readonly IRepository<SpecificaPars, Guid> _repository5;

        public HangfireService(IRepository<FreeActions, Guid> repository,
            IRepository<CommodInfo, Guid> repository0,
            IRepository<CommodityType, Guid> repository1,
            IRepository<CommodSpecifica, Guid> repository2,
           IRepository<CommodOrder, Guid> repository3,
            IRepository<UserOrder, Guid> repository4,
            IRepository<SpecificaPars, Guid> repository5
            )

        {
            _repository = repository;
            _repository0 = repository0;
            _repository1 = repository1;
            _repository2 = repository2;
            _repository3 = repository3;
            _repository4 = repository4;
            _repository5=repository5;
    }
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            RecurringJob.AddOrUpdate(() => HangfireJob(), Cron.Minutely());

            return Task.CompletedTask;
        }

        public void HangfireJob()
        {
            foreach (var item in _repository3)
            {
                if (item.OrderInfoState ==(CommodOrders.OrderInfoStateEnum)10)
                {
                    var m = DateTime.Now.AddDays(1) - DateTime.Now;
                    int s = DateTime.Compare(DateTime.Now, item.CreationTime.AddDays(1));
                    if (s >= 0)
                    {
                        Guid op = new Guid(item.OrderInfoWaybillNum);

                        List<CommodInfo> k = (from a in _repository0
                                              join b in _repository4 on a.Id.ToString() equals b.GoodId.ToString()
                                              orderby a.CommodityName
                                              select a).ToList();

                        List<UserOrder> k2 = (from a in _repository0
                                              join b in _repository4 on a.Id.ToString() equals b.GoodId.ToString()
                                              orderby a.CommodityName
                                              select b).ToList();
                        for (int i = 0; i < k.Count(); i++)
                        {
                            Guid c = _repository5.Where(s => s.orderSpId ==Convert.ToString(k[i].Id) && s.SpName ==k2[i].goodsku).ToList()[0].Id;
                            var dic = AsyncHelper.RunSync(() => { return _repository5.GetAsync(c); });
                            dic.Proid = dic.Proid + k2[i].Num;
                        }
                        var dic2 = AsyncHelper.RunSync(() => { return _repository3.GetAsync(item.Id); });
                        dic2.OrderInfoState=(CommodOrders.OrderInfoStateEnum)50;

                    }
                    else
                    {

                    }
                }
            }


        }
    }



}

